package com.lz.service.impl;

import com.lz.dao.UserDao;
import com.lz.pojo.User;
import com.lz.service.UserService;
import com.lz.util.Constants;
import com.lz.util.Funcs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public User getUesrByNameAndPassword(String username, String password)throws Exception{

        User user=userDao.getUesrByNameAndPassword(username);
        if (user==null){
             throw new Exception("用户名不存在");
        }else{
            password= Funcs.MD5(password+ Constants.PASSWORD_SALT_KEY);
            if (user.getPassword().equals(password)){
                user.setPassword(null);
                return user;
            }
        }
        throw new Exception("用户名密码错误");
    }


    @Override
    public int getUserByName(String username) {
        return userDao.getUserByName(username);
    }

    @Override
    public void addUser(User user) {
          user.setPassword(Funcs.MD5(user.getPassword()+Constants.PASSWORD_SALT_KEY));
           userDao.addUser(user);
    }
}
